﻿using Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SchemaMonitor.html
{
	[Serializable]
	public class CThread
	{
        //Read Data
		public string Users;
        public List<CMessage> Messages;

        //Computed data
        public string UserNames;
        public Guid HashMd5;
        public CMessage First {  get { return Messages.Count == 0 ? null : Messages[Messages.Count - 1]; } }
        public CMessage Last  { get { return Messages.Count == 0 ? null : Messages[0]; } }


        public List<string> SetUserNames(CUser u, Dictionary<long, string> dict)
        {
            var ids = UserIdsAsList;
            var names = new List<string>(ids.Count);
            var wc = new CWebClient(20000);
            var jss = new System.Web.Script.Serialization.JavaScriptSerializer();

            foreach (var i in ids)
            {
                if (! dict.ContainsKey(i))
                {
                    var url = string.Concat("https://graph.facebook.com/", i, "?access_token=", u.UserToken);//, "&metadata=1");
                    try
                    {
                        var d = CEngine.DownloadDict(url, wc);
                        var name = (string)d["name"];
                        dict.Add(i, name);
                    }
                    catch
                    {
                        dict.Add(i, i.ToString());
                    }
                }
                names.Add(dict[i]);
            }
            this.UserNames = CUtilities.ListToString(names);
            this.HashMd5 = new Guid(CBinary.MD5(string.Concat(this.Users + First.Date)));
            return names;
        }
        public Dictionary<string, object> AsDict()
        {
            var profiles = new Dictionary<string, object>();

            var d = new Dictionary<string, object>();
            d.Add("id", json.CNodeRoot.GuidToStr(HashMd5));

            var to = new List<object>();
            for (var i=0; i<UserIdsAsList.Count; i++)
            {
                var names = CUtilities.SplitOn(UserNames, ",");
                var id = UserIdsAsList[i];
                var name = names[i];
                var email = UsersAsList[i];
                var u = new Dictionary<string, object>() {
                    { "id", id },
                    { "name", name },
                    { "email", email }
                };
                to.Add(u);

                profiles.Add(email, u);
            }
            d.Add("to", to.ToArray());

            var comments = new List<object>();
            foreach (var i in Messages)
            {
                var m = new Dictionary<string, object> {
                    { "message", i.Message },
                    { "created_time", i.Date },
                    { "from", profiles.ContainsKey(i.User) ? profiles[i.User] : new Dictionary<string, object> { { "name", i.User} } },
                };
            }
            d.Add("comments", comments.ToArray());

            return d; 
        }

        public List<string> UsersAsList
		{
			get
			{
				return CUtilities.StringToListStr(this.Users);
			}
        }
        private List<long> UserIdsAsList
        {
            get
            {
                var emails = UsersAsList;
                var ids = new List<long>(emails.Count);
                foreach (var i in emails)
                {
                    var s = i.Substring(0, i.IndexOf("@"));
                    ids.Add(long.Parse(s));
                }
                return ids;
            }
        }
    }
}
